using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Adge.BLL;
using Adge.Models;
using NHibernate.Expression;
using Adge.Common;
using System.Collections.Generic;

namespace Adge.Web.UI.Admin
{
    public partial class CategoryCenter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lnkNewsEdit.NavigateUrl += Utils.COMMA + Request.QueryString;
                InitInfos();
                InitTree();
            }
        }

        private void InitTree()
        {
            Adge.BLL.AdgeArBase<CategoryInfo> catTree = new Adge.BLL.AdgeArBase<CategoryInfo>();
            CategoryInfo[] cats = catTree.FindAll();

            foreach (CategoryInfo cat in cats)
            {
                if (cat.CatParentId == 0)
                {
                    TreeNode newNode = CreateNode(cat.Catname, cat.CatId.ToString(), true);
                    tvCategory.Nodes.Add(newNode);
                    PopulateSubTree(cat.Children, newNode);
                }
            }
        }

        private void PopulateSubTree(IList<CategoryInfo> dbRow, TreeNode node)
        {
            if (dbRow != null)
            {
                foreach (CategoryInfo cat in dbRow)
                {
                    TreeNode childNode = CreateNode(cat.Catname, cat.CatId.ToString(), true);
                    node.ChildNodes.Add(childNode);
                    PopulateSubTree(cat.Children, childNode);
                }
            }
        }

        private TreeNode CreateNode(string text, string value, bool expanded)
        {
            TreeNode node = new TreeNode();
            node.Text = text;
            node.Value = value;
            //node.ImageUrl = imageurl;
            node.Expanded = expanded;
            node.NavigateUrl = "CategoryEdit.aspx?dataid=" + value;
            return node;
        }

        private void InitInfos()
        {
            Adge.BLL.AdgeArBase<CategoryInfo> catlist = new Adge.BLL.AdgeArBase<CategoryInfo>();
            
            #region Bind Catgory
            
            ICriterion[] criterias = new ICriterion[] { };
            CategoryInfo [] entities = catlist.SlicedFindAll(0, 100, new Order[] { }, criterias);
            gvNewsCenter.DataSource = entities;
            gvNewsCenter.DataBind();
            #endregion
        }

        protected void gvNewsCenter_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvNewsCenter.PageIndex = e.NewPageIndex;
            InitInfos();
        }

        protected void gvNewsCenter_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            object key = gvNewsCenter.DataKeys[e.RowIndex].Value;
            Adge.BLL.AdgeArBase<CategoryInfo> catlist = new Adge.BLL.AdgeArBase<CategoryInfo>();
            if (catlist.GetEntityById(key) != null && catlist.GetEntityById(key).CatParentId > 0)
            {
                catlist.Delete(key);
                InitInfos();
            }
        }
    }
}
